package com.foreknow.demo4;

import java.util.Scanner;

public class Example1 {
    public static void welcome() {
        System.out.println("---------------------------");
        System.out.println("-------------欢迎登录--------------");
        System.out.println("-------------银行系统--------------");
        System.out.println("---------------------------");
    }

    /**
     * 登录方法
     * @param user
     * @param pwd
     * @return
     */
    public static boolean login(String user,String pwd) {
        Scanner input = new Scanner(System.in);
        for(int i = 3;i>0;i--){
            System.out.println("请输入账号");
            String new_user = input.next();
            System.out.println("请输入密码");
            String new_pwd = input.next();
            if(user.equals(new_user) && pwd.equals(new_pwd)) {
                //登录成功
                System.out.println("登录成功");
                return true;
            }else {
                if(i-1==0){
                    System.out.println("密码错误3次");
                    System.exit(0);
                }
                System.out.println("账号名或密码错误，请重新输入今日还有"+(i-1)+"次机会");
            }
        }
        return false;
    }

    /**
     * 查询余额
     * @param money
     */
    public static void checkMoney(double money) {
        System.out.println("您的余额为"+money);
    }

    /**
     * 存款
     * @param money
     * @return
     */
    public static double setMoney(double money) {
        System.out.println("请输入您要存款的金额");
        Scanner sc = new Scanner(System.in);
        double addMoney = sc.nextInt();
        if(addMoney > 10000) {
            System.out.println("单次存款金额超过1万元");
            return 0;
        }else if(addMoney < 0) {
            System.out.println("存款金额错误");
            return 0;
        }else if(addMoney % 100 !=0) {
            System.out.println("请输入100的整数倍存款");
            return 0;
        } else {
            System.out.println("存款成功，当前的余额为"+(money+addMoney));
            return addMoney;
        }
    }

    /**
     * 取款
     * @param money
     * @return
     */
    public static double getMoney(double money) {
        System.out.println("请输入您要取款的金额");
        Scanner sc = new Scanner(System.in);
        double subMoney = sc.nextInt();
        if(subMoney > money) {
            System.out.println("余额不足");
            return 0;
        }else if(subMoney < 0) {
            System.out.println("取款金额错误");
            return 0;
        }else if(subMoney > 3000) {
            System.out.println("单次取款金额超过3000");
            return 0;
        } else if(subMoney % 100 !=0) {
            System.out.println("请取出100的整数倍存款");
            return 0;
        } else {
            System.out.println("取款成功，当前的余额为"+(money-subMoney));
            return subMoney;
        }
    }

    /**
     * 判断是否继续操作
     */
    public static void decide() {
        Scanner input = new Scanner(System.in);
        System.out.println("是否继续操作(y/n)");
        String dec = input.next();
        if(dec.equals("y")) {
            return;
        }else if(dec.equals("n")) {
            System.out.println("退出成功");
            System.exit(0);
        } else {
            while (true){
                System.out.println("输入错误，请重新输入 是否继续(y/n)");
                String temp = input.next();
                if(temp.equals("y")) {
                    return;
                }else if(temp.equals("n")) {
                    System.out.println("退出成功");
                    System.exit(0);
                }
            }
        }
    }

    /**
     * 修改密码
     * @param pwd
     * @return
     */
    public static String changePwd(String pwd) {
        System.out.println("请输入旧密码");
        Scanner input = new Scanner(System.in);
        String old_pwd = input.next();
        if(old_pwd.equals(pwd)) {
            System.out.println("请输入新密码");
            String new_pwd = input.next();
            System.out.println("请再次输入新密码");
            String new_pwd1 = input.next();
            if(new_pwd.equals(new_pwd1)) {
                System.out.println("修改密码成功");
                return new_pwd;
            }else {
                System.out.println("两次输入不一致，请重新输入");
                return pwd;
            }
        }else {
            System.out.println("旧密码输入错误");
        }
        return pwd;
    }

    public static void main(String[] args) {
        //初始化用户的信息
        String user = "admin";
        String pwd = "123";
        double money = 10000;
        Scanner input = new Scanner(System.in);
        welcome();
        boolean isRight = login(user,pwd);
        while (isRight){
            //如果登录成功了，进入的操作界面
            while(true) {
                System.out.println("1. 查询余额  2. 存款  3. 取款  4. 修改密码   5.退出");
                int opt = input.nextInt();
                switch (opt){
                    case 1:
                        //查询余额
                        checkMoney(money);
                        //判断用户是否继续
                        decide();
                        break;
                    case 2:
                        //存款
                        money += setMoney(money);
                        decide();
                        break;
                    case 3:
                        //取款
                        money -= getMoney(money);
                        decide();
                        break;
                    case 4:
                        //修改密码
                        String str = changePwd(pwd);
                        System.out.println("当前的密码"+str);
                        decide();
                        break;
                    case 5:
                        System.out.println("退出成功");
                        System.exit(0);

                }
            }
        }

    }
}
